package server

import (
	"gorm.io/gorm"
	"user_server/basic/config"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30);not null;comment:'用户名称'"`
	Password string `gorm:"type:char(32);not null;comment:'密码'"`
}

func (u *User) UserLogins() (uint, string) {
	var user User
	config.DB.Where("username=?", u.Username).First(&user)
	if user.ID == 0 {
		err := config.DB.Create(&u).Error
		if err != nil {
			return 0, "注册失败"
		}
		return u.ID, ""
	} else {
		if u.Password != user.Password {
			return 0, "密码错误"
		}
		return user.ID, ""
	}
}
